
program uni_menu

  declare
  global numeric t1i
  global numeric t1o
  global numeric winf
  global numeric winsub
  global numeric win1
  global numeric win2
  global numeric win3
  global numeric win4
  global numeric led_win
  global numeric crt_win
  global numeric maxtests = 12                  ! runs 12 test types
! title_msg appears on top line of menu
  global string title_msg = "UNIVERSAL STATION DIAGNOSTICS"
  global string clr_scrn = "\1B[2J"
  global string cur_on = "\1B[?25h"
  global string cur_off = "\1B[?25l"
  global string rev = "\1B[7m"
  global string norm = "\1B[0m"
  global string bold = "\1B[1m"
  global string lf_on = "\1B[20h"
  global string lf_off = "\1B[20l"
  global string array[1:3] fmsg
  global string array[1:21] smsg
  global string array[1:17] tmsg
  persistent numeric cntrl_all = 0
end declare

!@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
function purge
  declare global numeric t1i
  char = ""
  loop while poll (t1i,"input")
    input on t1i, char
  end loop
end function
!@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

!@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
! This function will display a decision window only for the automatic
! test. Choose are "Repeat
function fail_win
  declare
        global numeric t1i
        global numeric winf
        global string rev = "\1B[7m"
        global string norm = "\1B[0m"
        global string array[1:3] fmsg
  end declare

  print using "\1B[2;1H"+rev+"?#"+norm,on winf,fmsg[1]
  print using "\1B[3;1H?#",on winf,fmsg[2]
  print using "\1B[4;1H?#",on winf,fmsg[3]

  winctl channel winf, position "unhide"

  tcnt = 1                                 ! ** Set tp first menu selection
  char = ""
  loop until (char = chr( $D))
    input on t1i, char

    if (char = chr($A1)) then                 !up arrow
        print using "\1B[?#;1H?#",on winf,tcnt+1,fmsg[tcnt]
        if (tcnt = 1) then
          tcnt = 3
        else
          tcnt = tcnt - 1
        endif
        print using "\1B[?#;1H"+rev+"?#"+norm,on winf,tcnt+1,fmsg[tcnt]
    elseif (char = chr($A2))then              !down arrow
        print using "\1B[?#;1H?#",on winf,tcnt+1,fmsg[tcnt]
        if (tcnt = 3) then
          tcnt = 1
        else
          tcnt = tcnt + 1
        endif
        print using "\1B[?#;1H"+rev+"?#"+norm,on winf,tcnt+1,fmsg[tcnt]
    endif

  end loop

  winctl channel winf, position "hide"
  return(tcnt)

end function
!@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

!@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
function pf_cntrl (wndw,p_f,pos)
  declare
        numeric wndw
        string p_f
        numeric pos
        global numeric t1i
        global numeric win1
        global numeric winsub
        global numeric winf
        persistent numeric cntrl_all
  end declare

  if (p_f = "PASSED") then
    if (wndw = 1) then
        print using "\1B[?#;25HPASSED ",on winsub,pos+1
    else
        print using "\1B[?#;25HPASSED ",on win1,pos+1
    endif
    pos = pos + 1
  else
    for x = 1 to 15
      print on winf, "\07"
    next
    if (wndw = 1) then
        print using "\1B[?#;25H\1B[5mFAILED \1B[0m",on winsub,pos+1
    else
        print using "\1B[?#;25H\1B[5mFAILED \1B[0m",on win1,pos+1
    endif
!    for x = 1 to 15
!      print on win1, "\07"
!    next
    if ((cntrl_all & $1) = 0) then
      x = fail_win()
      if (x = 1) then
        print using "\1B[?#;25H      ",on win1,pos+1
      elseif (x = 2) then
        pos = pos + 1   !continue testing
      elseif (x = 3) then
        pos = 49        !quit testing
      endif
    endif
  endif

  return(pos)

end function
!@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

!@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
function sub_tst(code_str)
  declare
        string code_str
        string temp
        numeric offset                  !offset = 1 minus the first msg
        numeric numtst                  !numtst = the number uf subtset
        numeric fflag                   !fflag = 1 if a test fails
        global numeric t1i
        global numeric winsub
        global string array[1:21] smsg
        persistent numeric cntrl_all
  end declare

  temp = mid(code_str,3,2)
  offset = val(temp,10)
  temp = mid(code_str,6,2)
  numtst = val(temp,10)

  winctl channel winsub, position "unhide"
  print on winsub,"\1B[2J"                !clear window
!  print using "\1B[2;4Hoffset = ?#  numtst = ?#",on winsub,offset,numtst


  for scnt = 1 to numtst
        print using "\1B[?#;4H?#",on winsub,scnt+1,smsg[scnt+offset]
  next

!  winctl channel winsub, position "unhide"

  scnt = 1
  fflag = 2

  if (cntrl_all & 1) then
    print using "\1B[2;4H\1B[7m?#\1B[0m",on winsub,smsg[offset+1]
    print using "\1B[?#;4H?#",on winsub,numtst+2,smsg[offset+numtst+1]

    loop until (scnt > numtst)

      char = ""
      loop until (char = chr( $D))
        input on t1i, char

        if (char = chr($A1)) then                 !up arrow
          print using "\1B[?#;4H?#",on winsub,scnt+1,smsg[scnt+offset]
          if (scnt = 1) then
            scnt = numtst+1
          else
            scnt = scnt - 1
          endif
          print using "\1B[?#;4H\1B[7m?#\1B[0m",on winsub,scnt+1,smsg[scnt+offset]
        elseif (char = chr($A2))then              !down arrow
          print using "\1B[?#;4H?#",on winsub,scnt+1,smsg[scnt+offset]
          if (scnt = numtst+1) then
            scnt = 1
          else
            scnt = scnt + 1
          endif
          print using "\1B[?#;4H\1B[7m?#\1B[0m",on winsub,scnt+1,smsg[scnt+offset]
        endif
      end loop

      if (scnt < numtst+1) then
        print using "\1B[?#;25H\1B[1;5mTesting\1B[0m",on winsub,scnt+1
        tst_result = uni_sub(1,scnt+offset)
        if (tst_result = "PASSED") then
          fflag = fflag & 1
        else
          fflag = 1
        endif
        pf_cntrl(1,tst_result,scnt)
      endif
    end loop

  else !^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    loop until (scnt > numtst)
      print using "\1B[?#;25H\1B[1;5mTesting\1B[0m",on winsub,scnt+1
      tst_result = uni_sub(1, scnt+offset)
      if (tst_result = "PASSED") then
        fflag = fflag & 1
      else
        fflag = 1
      endif
      scnt = pf_cntrl(1,tst_result,scnt)
    end loop
  endif

  winctl channel winsub, position "hide"

  if (scnt = 49) then
      return("return")
  endif
  if (fflag = 1) then
    return("FAILED")
  elseif (fflag = 0) then
    return("PASSED")
  else
    return(" ")
  endif
end function
!@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

!@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
function menu1
  declare
        global numeric t1i
        global numeric win1
        global numeric maxtests
        global string array[1:17] tmsg
        persistent numeric cntrl_all
  end declare

  cntrl_all = cntrl_all & $FE           !don't display fault windows
  print on win1,"\1B[2J"                !clear window
  for tcnt = 1 to maxtests
        print using "\1B[?#;4H?#",on win1,tcnt+1,tmsg[tcnt]
  next

  winctl channel win1, position "unhide"

  tcnt = 1
  loop until (tcnt > maxtests)
    print using "\1B[?#;25H\1B[1;5mTesting\1B[0m",on win1,tcnt+1
    tst_result = uni_sub(0,tcnt)
    temp = mid(tst_result,1,1)
    if (temp = "@") then
      tst_result = sub_tst(tst_result)
    endif
    if (tst_result = "return") then
      tcnt = 49
    else
      tcnt = pf_cntrl(0,tst_result,tcnt)
    endif
  end loop

  if(tcnt <> 49) then
    print on win1,"\1B[17;2HPress F5 to return to menu."
    char = ""
    loop until (char = chr( $86))         !$86 = F5 KEY
      input on t1i, char
    end loop
  endif

  winctl channel win1, position "hide"

end function
!@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

!@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
function menu2
  declare
        global numeric t1i
        global numeric win1
        global numeric maxtests
        global string rev = "\1B[7m"
        global string norm = "\1B[0m"
        global string array[1:17] tmsg
        string temp
        persistent numeric cntrl_all
  end declare

  cntrl_all = cntrl_all |  $1           !display fault windows
  print on win1,"\1B[2J"                !clear window
  print using "\1B[2;4H"+rev+"?#"+norm,on win1,tmsg[1]
  for tcnt = 2 to maxtests+1
        print using "\1B[?#;4H?#",on win1,tcnt+1,tmsg[tcnt]
  next

  winctl channel win1, position "unhide"

  tcnt = 1      ! ** Set to first menu item **
  loop until (tcnt > maxtests)

    char = ""
    loop until (char = chr( $D))
      input on t1i, char

      if (char = chr($A1)) then                 !up arrow
        print using "\1B[?#;4H?#",on win1,tcnt+1,tmsg[tcnt]
        if (tcnt = 1) then
          tcnt = maxtests+1
        else
          tcnt = tcnt - 1
        endif
        print using "\1B[?#;4H"+rev+"?#"+norm,on win1,tcnt+1,tmsg[tcnt]
      elseif (char = chr($A2))then              !down arrow
        print using "\1B[?#;4H?#",on win1,tcnt+1,tmsg[tcnt]
        if (tcnt = maxtests+1) then
          tcnt = 1
        else
          tcnt = tcnt + 1
        endif
        print using "\1B[?#;4H"+rev+"?#"+norm,on win1,tcnt+1,tmsg[tcnt]
      endif

    end loop

    if (tcnt < maxtests+1) then
      print using "\1B[?#;25H\1B[1;5mTesting\1B[0m",on win1,tcnt+1
      tst_result = uni_sub(0,tcnt)
      temp = mid(tst_result,1,1)
      if (temp = "@") then
        tst_result = sub_tst(tst_result)
      endif
      if(tst_result = " ") then
        print using "\1B[?#;25H       ",on win1,tcnt+1
      else
        pf_cntrl(0,tst_result,tcnt)
      endif
    endif
  end loop

  winctl channel win1, position "hide"

end function
!@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

!@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
function menu3
  declare
        global numeric t1i
        global numeric win3
  end declare


  print on win3,"\1B[2J\1B[3;2HLoading UFI menu program ..."
  winctl channel win3, position "unhide"

!  clb_ufi()

  print on win3,"\1B[2J\1B[3;2HPress F5 to return to menu. "

  char = ""
  loop until (char = chr( $86))         !$86 = F5 KEY
        input on t1i, char
  end loop

  winctl channel win3, position "hide"

end function
!@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

!@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
function menu4
  declare
        global numeric t1i
        global numeric win4
  end declare

  char = ""
  winctl channel win4, position "unhide"

  loop until (char = chr( $86))         !$86 = F5 KEY
        input on t1i, char
  end loop

  winctl channel win4, position "hide"

end function
!@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

open()
t1i = open device "/term1", as "input", mode "unbuffered"
t1o = open device "/term1", as "output",mode "unbuffered"
led_win = open device "/term1/win",as "output",mode "unbuffered"
crt_win = open device "/term2/win",as "output",mode "unbuffered"


winm = open device "/term2/win",as "output",xorg 0,yorg 0,xdim 80,ydim 3,border title_msg
winctl channel winm, position "hide"
print on winm, cur_off+lf_on+norm

  tmenu1 = "       "+rev+"AUTO TEST"+norm+"        MANUAL TEST        UFI        HELP        EXIT       "
  tmenu2 = "       AUTO TEST        "+rev+"MANUAL TEST"+norm+"        UFI        HELP        EXIT       "
  tmenu3 = "       AUTO TEST        MANUAL TEST        "+rev+"UFI"+norm+"        HELP        EXIT       "
  tmenu4 = "       AUTO TEST        MANUAL TEST        UFI        "+rev+"HELP"+norm+"        EXIT       "
  tmenu5 = "       AUTO TEST        MANUAL TEST        UFI        HELP        "+rev+"EXIT"+norm+"       "

winsub = open device "/term2/win",as "output",xorg 36,yorg 4,xdim 34,ydim 9,border "SUBTESTS"
winctl channel winsub, position "hide"
print on winsub, cur_off+lf_on+norm
! Each set of labels for a submenu must have "Return to test menu" after it.
! array for submenu labels
  smsg[1]  = "Vector I/O Loopback:"
  smsg[2]  = "   Probe Vector I/O:"
  smsg[3]  = "Vector I/O 1&2 Ctrl:"
  smsg[4]  = "Return to test menu "
  smsg[5]  = "Vector I/O 3&4 Ctrl:"
  smsg[6]  = "    Clock Frequency:"
  smsg[7]  = "Return to test menu "
  smsg[8]  = "  Power Supply Test:"
  smsg[9]  = "    ROM Module Test:"
  smsg[10] = "            Later!!:"
  smsg[11] = "Return to test menu "
  smsg[12] = "               Test:"
  smsg[13] = "               Test:"
  smsg[14] = "               Test:"
  smsg[15] = "Return to test menu "

winf = open device "/term2/win",as "output",xorg 40,yorg 12,xdim 21,ydim 7,border "FAIL WINDOW"
winctl channel winf, position "hide"
print on winf, cur_off+lf_on+norm

!  array for failure window
  fmsg[1] = "   Repeat test     "
  fmsg[2] = " Continue testing  "
  fmsg[3] = "Return to main menu"

win1 = open device "/term2/win",as "output",xorg 0,yorg 3,xdim 34,ydim 20,border ""
winctl channel win1, position "hide"
print on win1, cur_off+lf_on+norm

! maxtests = the number of tests in tmsg array, not counting "Return to menu"
! "Return to menu" must be the last entry in tmsg array
! maxtests must be 16 or less for the exsisting window size
! actual test calls are made from uni_sub(), modify this file accordingly
!  array for test labels
  tmsg[1]  = "VECTOR I/O Loopback:"
  tmsg[2]  = "   Probe VECTOR I/O:"
  tmsg[3]  = " VECTOR I/O 1&2 CLK:"
  tmsg[4]  = " VECTOR I/O 3&4 CLK:"
  tmsg[5]  = "   CLOCK FREQ. TEST:"
  tmsg[6]  = "  POWER SUPPLY TEST:"
  tmsg[7]  = "             LATER!:"
  tmsg[8]  = "                   :"
  tmsg[9]  = "                   :"
  tmsg[10] = "                   :"
  tmsg[11] = "                   :"
  tmsg[12] = "                   :"
  tmsg[13] = "Return to main menu "

win2 = open device "/term2/win",as "output",xorg 10,yorg 5,xdim 63,ydim 18,border "TEST WINDOW"
winctl channel win2, position "hide"
print on win2, cur_off+lf_on+norm

win3 = open device "/term2/win",as "output",xorg 38,yorg 5,xdim 32,ydim 5,border "UFI WINDOW"
winctl channel win3, position "hide"
print on win3, cur_off+lf_on+norm

win4 = open device "/term2/win",as "output",xorg 0,yorg 5,xdim 80,ydim 15,border "HELP WINDOW"
winctl channel win4, position "hide"

print on win4, cur_off+lf_on+norm
print on win4,"\1B[2;2H"+bold+"AUTO TEST"+norm+": This selection will run all the tests automaticly."
print on win4,"\1B[3;13HIt will stop when a test has failed."
print on win4,"\1B[4;2H"+bold+"MANUAL TEST"+norm+": This selection will run a user selected test."
print on win4,"\1B[5;2H"+bold+"UFI"+norm+": This selection will load and run the unguided fault test."
print on win4,"\1B[6;2H"+bold+"HELP"+norm+": This selection will display the help window."
print on win4,"\1B[7;2H"+bold+"EXIT"+norm+": This selection will exit this program."
print on win4,"\1B[9;20HPress F5 to return to menu."


print using "?#",on winm,"\1B[1;1H"+tmenu1
winctl channel winm, position "unhide"

done = 0
track = 1

purge()
loop until done

  key_pressed = ""

  loop until (key_pressed = chr( $D))
     input on t1i, key_pressed

     if (track = 1) and (key_pressed = chr($A3))then
       print using "?#",on winm,"\1B[1;1H"+tmenu2
       track = 2
     elseif (track = 1) and (key_pressed = chr($A4))then
       print using "?#",on winm,"\1B[1;1H"+tmenu5
       track = 5
     elseif (track = 2) and (key_pressed = chr($A3)) then
       print using "?#",on winm,"\1B[1;1H"+tmenu3
       track = 3
     elseif (track = 2) and (key_pressed = chr($A4)) then
       print using "?#",on winm,"\1B[1;1H"+tmenu1
       track = 1
     elseif (track = 3) and (key_pressed = chr($A3)) then
       print using "?#",on winm,"\1B[1;1H"+tmenu4
       track = 4
     elseif (track = 3) and (key_pressed = chr($A4)) then
       print using "?#",on winm,"\1B[1;1H"+tmenu2
       track = 2
     elseif (track = 4) and (key_pressed = chr($A3)) then
       print using "?#",on winm,"\1B[1;1H"+tmenu5
       track = 5
     elseif (track = 4) and (key_pressed = chr($A4)) then
       print using "?#",on winm,"\1B[1;1H"+tmenu3
       track = 3
     elseif (track = 5) and (key_pressed = chr($A3)) then
       print using "?#",on winm,"\1B[1;1H"+tmenu1
       track = 1
     elseif (track = 5) and (key_pressed = chr($A4)) then
       print using "?#",on winm,"\1B[1;1H"+tmenu4
       track = 4

     endif

   end loop
     if (track = 1) then
        menu1()                  ! AUTO TEST
     elseif (track = 2) then
        menu2()                  ! MANUAL TEST
     elseif (track = 3) then
        menu3()                  ! UFI TEST
     elseif (track = 4) then
        menu4()                  ! HELP MENU
     elseif (track = 5) then
        done = 1

     endif

end loop

close (t1i)
close (t1o)
close (winm)
close (winf)
close (win1)
!close (win2)      **   USED IN FOLLOWING PROGRAMS **
close (win3)
close (win4)
end program

